package com.spring.jdbctemplate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class Main {
	
	private  JdbcTemplate jdbcTemplate; 
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("jdbctemplate.xml");
		JdbcTemplate p = (JdbcTemplate) ctx.getBean("jdbcTemplate");
		query(p);
		batinsert(p);
	}
	
	
	public static void query(JdbcTemplate p ){
		String sql = "select * from role";
		RowMapper<Role> rowMapper = new BeanPropertyRowMapper<Role>(Role.class);
				List<Role> list = 	p.query(sql,  rowMapper);
				for (Role role : list) {
					System.out.println(role.getRoleName());
				}
	}
	
	public static void batinsert(JdbcTemplate p ){
		String sql = "insert into  role(id,role_name) values (?,?)";
		List<Object[]> list = new ArrayList<Object[]>();
		list.add(new Object[]{"3","liquan"});
		list.add(new Object[]{"4","li444"});
		p.batchUpdate(sql, list);
	}
}
